/*
 * StartupFrameBackend.java
 * Team qq 2011
 */
package com.google.code.timetrail.presenter;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import com.google.code.timetrail.backend.Control;
import com.google.code.timetrail.backend.Doctor;
import com.google.code.timetrail.backend.Engineer;
import com.google.code.timetrail.backend.Entrepreneur;
import com.google.code.timetrail.backend.Inventory;
import com.google.code.timetrail.backend.Nutritionist;
import com.google.code.timetrail.backend.Person;

/**
 *  @author jake.gibel@gmail.com
 *  @version 1.0.0
 */
public class StartupFrameBackend {
    /**
     * the Game information
     */
    private final Control gameControl;

    /**
     * a List of the names of the other players in your party
     */
    private final List<String> additionalPlayers;

    /**
     * The number of times you have attempted to add a player
     */
    private int addButtonCount;

    /**
     * the previously added additional player
     */
    private String addedPlayer;

    /**
     * The profession you had selected
     */
    private String selectedProfession;

    /**
     * The player's name
     */
    private String playerName;

    /**
     * The instance of the player
     */
    private Person player;

    /**
     * The Instance of the player's inventory
     */
    private Inventory playerInv;

    /**
     * the index that the player selected for profession
     */
    private int selectedProfessionIndex;

    /**
     * Creates a new StartupFrameBackend
     * @param gameControl the game's Control instance
     */
    public StartupFrameBackend(Control gameControl){
        this.gameControl = gameControl;
        additionalPlayers = new ArrayList<String>();
        selectedProfessionIndex = -1;
    }

    /**
     * @return the Text that goes in TitleLabel
     */
    public String getTitleLabel() {

        return "Time Trail \n";
    }

    /**
     * @return the text that goes in the EnterNameLabel
     */
    public String getEnterNameLabel() {

        return "Enter Name:";
    }

    /**
     * @return The default player name
     */
    public String getDefaultPlayerName() {

        return "";
    }

    /**
     * @return the text that goes in AddMembersLabel
     */
    public String getAddMembersLabel() {

        return "Enter Name of additional Party members:";
    }

    /**
     * @return The text that goes into the AddButton
     */
    public String getAddButtonText() {

        return "Add Player";
    }

    /**
     * This is the button count for the pressing add button
     */
    private static final int BUTTONCOUNT = 4;
    
    /**
     * press the add button, OptionPane shows up and it adds it if
     * there's less than four already added
     */
    public void pressAddButton() {
    	addedPlayer = null;
        if(addButtonCount < BUTTONCOUNT){
        	
        	final JFrame frame = null;
           
            											
        	final String s = JOptionPane.showInputDialog(
                                frame,
                                "Enter Name of additional Party member",
                                "Name"
                                );
            if ((s != null) && (s.length() > 0)) {
                addedPlayer = s;
                additionalPlayers.add(s);
                addButtonCount++;
                return;
            }
        }
        
    }

    /**
     * @return the previously added player
     */
    public String getAddedPlayer() {
        return addedPlayer;
    }

    /**
     * @return if the player name is valid
     */
    public boolean canAddPlayer(){
        return addedPlayer != null && !addedPlayer.isEmpty();
    }

    //Following Constant Names are great whoever made these pro edits sure doesn't
    //need to re-evaluate their naming practices 
    /**
     * The selection zero for the professional value
     */
    private static final int SELECTIONZERO = 0;
    
    /**
     * The selection one for the professional value
     */
    private static final int SELECTIONONE = 1;
    
    /**
     * The selection two for the professional value
     */
    private static final int SELECTIONTWO = 2;
    
    /**
     * The selection three for the professional value
     */
    private static final int SELECTIONTHREE = 3;
    
    /**
     * changes the selected profession to the index
     * @param selectedIndex the index that is selected for Profession
     */
    public void professionValueChanged(int selectedIndex) {
        this.selectedProfessionIndex = selectedIndex;

        if(selectedIndex == SELECTIONZERO){
            selectedProfession = "Engineer";
        } else if(selectedIndex == SELECTIONONE){
            selectedProfession = "Nutritionist";
        } else if(selectedIndex == SELECTIONTWO){
            selectedProfession = "Doctor";
        } else if(selectedIndex == SELECTIONTHREE){
            selectedProfession = "Entrepreneur";
        }
        
    }

    /**
     * @return An array of possible Professions
     */
    public String[] getProfessionValues() {

        return new String[] {"Engineer", "Nutritionist", "Doctor", "Entrepreneur"};
    }

    /**
     * @return The text of the ProfessionLabel
     */
    public String getProfessionLabelText() {

        return "Choose Profession:";
    }

    /**
     * @return the text that is in the next button
     */
    public String getNextButtonText() {

        return "Next >>";
    }

    /**
     * Sets the player's name to a String
     * @param text the new player's name
     */
    public void setNameText(String text) {

        playerName = text;
    }

    /**
     * determine if all information is complete, then sets the 
     * gameControl's data to these values if possible
     * @return true if all required information on the frame
     */
    public boolean canPressNextButton() {

    	final JFrame frame = null;
        if(playerName.equals("") || playerName == null || selectedProfessionIndex == -1){
            JOptionPane.showMessageDialog(frame,
                    "Please fill in all required information.",
                    "Some fields were left empty",
                    JOptionPane.PLAIN_MESSAGE);
            return false;
        } else {
            if(selectedProfession.equals("Engineer")){
                player = new Engineer();
            } else if(selectedProfession.equals("Nutritionist")){
                player = new Nutritionist();
            } else if(selectedProfession.equals("Doctor")){
                player = new Doctor();
            } else if(selectedProfession.equals("Entrepreneur")){
                player = new Entrepreneur();
            }
            final List<Person> members = new ArrayList<Person>();
            player.setName(playerName);
            playerInv = new Inventory();
            player.initialBonus(playerInv);
            player.intializeSkills();
            gameControl.setInv(playerInv);
            gameControl.setPlayer(player);
            members.add(player);
            for(int i = 0; i < additionalPlayers.size(); i++){
                members.add(new Person(additionalPlayers.get(i)));
            }
            
            
            gameControl.setMembers(members);
            return true;
        }
    }

    /**
     * @return String representation of the StartupFrameBackend
     */
    public String toString(){
    	return "StartupFrameBackend";
    }
}
